package ar.edu.unicen.exa.isistan.archassis.javatoxml;

import harsh.javatoxml.Java2XML;
import harsh.javatoxml.XMLHelper;
import harsh.javatoxml.Exceptions.JXMLException;
import harsh.javatoxml.data.IXMLElement;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class JavaToXMLBatch {
	private String xmlFile;
	private List<String> javaFilePathList;
	private List<String> javaClassNameList;
	
	public JavaToXMLBatch(String xmlFile) {
		this.xmlFile = xmlFile;
		this.javaFilePathList = new ArrayList<String>();
		this.javaClassNameList = new ArrayList<String>();
	}
	
	public void add(String javaFilePath, String javaClassName) {
		javaFilePathList.add(javaFilePath);
		javaClassNameList.add(javaClassName);
	}
	
	
	public void convert() throws FileNotFoundException, JXMLException {
		//System.out.println(javaFilePathList.toString());
		Reader[] javaCode = new FileReader[javaFilePathList.size()];
		
		for (int i=0; i < javaFilePathList.size(); i++) {
			String file = javaFilePathList.get(i);
			File f = new File(file);
			if (f.isFile()) {
				//System.out.println("bien!");
			}
			javaCode[i] = new FileReader(javaFilePathList.get(i));
		}
		
		String[] classNames = new String[javaClassNameList.size()];
		javaClassNameList.toArray(classNames);
		try {
			IXMLElement element = Java2XML.convertToXML(javaCode, classNames);
			XMLHelper.dumpXMLToFile(element, xmlFile);
		} catch (Error e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// ejemplo de uso
		String pathProyecto = System.getProperties().get("user.dir").toString();
		JavaToXMLBatch jtb = new JavaToXMLBatch(pathProyecto + "/tmp/JAVA2XML.xml");
		
		
		jtb.add(pathProyecto + "/src/test/Test.java", "test.TestUU");
		jtb.add(pathProyecto + "/src/test/another/AnotherTest.java", "another.AnotherTestXX");
		
		try {
			jtb.convert();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JXMLException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
